﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Reflection;

namespace Viewer.BusinessObject.BusinessEntities
{
    [Serializable()]
    public partial class QUNhomQuyenByModule_Module : BusinessBase
    {
        public bool IsQuyenView { get; set; }
        public bool IsQuyenEdit { get; set; }
        public bool IsQuyenAdd { get; set; }
        public bool IsQuyenDelete { get; set; }

        public List<QUNhomQuyenByModule_Module> GetByNhomQuyenByModuleId(int pNhomQuyenByModuleId)
        {
            return this.Context.QUNhomQuyenByModule_Modules.Where(c => c.NhomQuyenByModuleID == pNhomQuyenByModuleId).ToList();
        }

        public QUNhomQuyenByModule_Module GetQuyenByModuleIDandUserID(int pModuleID, int pUserID)
        {
            var tmpListTongHopQuyen = (from nqmd_md in this.Context.QUNhomQuyenByModule_Modules
                                       join nqmd in this.Context.QUNhomQuyenByModules on nqmd_md.NhomQuyenByModuleID equals nqmd.NhomQuyenByModuleID
                                       join nqmd_nd in this.Context.QUNhomQuyenByModule_NguoiDungs on nqmd.NhomQuyenByModuleID equals nqmd_nd.NhomQuyenByModuleID
                                       where nqmd_nd.NguoiDungID == pUserID
                                       && nqmd_md.ModuleID == pModuleID
                                       select nqmd_md).ToList();

            QUNhomQuyenByModule_Module nhomQuyenTheoModule = new QUNhomQuyenByModule_Module();
            nhomQuyenTheoModule.IsQuyenAdd = false;
            nhomQuyenTheoModule.IsQuyenDelete = false;
            nhomQuyenTheoModule.IsQuyenEdit = false;
            nhomQuyenTheoModule.IsQuyenView = false;

            foreach (var item in tmpListTongHopQuyen)
            {
                nhomQuyenTheoModule.IsQuyenAdd = nhomQuyenTheoModule.IsQuyenAdd || item.IsAdd;
                nhomQuyenTheoModule.IsQuyenView = nhomQuyenTheoModule.IsQuyenView || item.IsView;
                nhomQuyenTheoModule.IsQuyenEdit = nhomQuyenTheoModule.IsQuyenEdit || item.IsEdit;
                nhomQuyenTheoModule.IsQuyenDelete = nhomQuyenTheoModule.IsQuyenDelete || item.IsDelete;
            }

            if (pUserID == 0)
            {
                nhomQuyenTheoModule.IsQuyenAdd = true;
                nhomQuyenTheoModule.IsQuyenDelete = true;
                nhomQuyenTheoModule.IsQuyenEdit = true;
                nhomQuyenTheoModule.IsQuyenView = true;
            }

            return nhomQuyenTheoModule;
        }
    }
}